package pa5.components;

import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.InlineDateField;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;

public class InputComponent extends CustomComponent implements ClickListener{

	@AutoGenerated
	private AbsoluteLayout mainLayout;
	@AutoGenerated
	private Button saveButton;
	@AutoGenerated
	private TextField resultField;
	@AutoGenerated
	private Label resultLabel;
	@AutoGenerated
	private InlineDateField dateInput;
	@AutoGenerated
	private Label player2Label;
	@AutoGenerated
	private TextField player2Field;
	@AutoGenerated
	private TextField player1Field;
	@AutoGenerated
	private Label player1Label;
	
	private Label testLabel;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public InputComponent() {
		buildMainLayout();
		setCompositionRoot(mainLayout);

		// TODO add user code here
	}

	@AutoGenerated
	private AbsoluteLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// player1Label
		player1Label = new Label();
		player1Label.setStyleName("inputLabel");
		player1Label.setImmediate(false);
		player1Label.setWidth("80px");
		player1Label.setHeight("18px");
		player1Label.setValue("Player 1");
		mainLayout.addComponent(player1Label, "top:42.0px;left:40.0px;");
		
		// player1Field
		player1Field = new TextField();
		player1Field.setImmediate(false);
		player1Field.setWidth("-1px");
		player1Field.setHeight("-1px");
		player1Field.setInvalidAllowed(false);
		player1Field.setRequired(true);
		player1Field.setSecret(false);
		mainLayout.addComponent(player1Field, "top:40.0px;left:140.0px;");
		
		// player2Field
		player2Field = new TextField();
		player2Field.setImmediate(false);
		player2Field.setWidth("-1px");
		player2Field.setHeight("-1px");
		player2Field.setRequired(true);
		player2Field.setSecret(false);
		mainLayout.addComponent(player2Field, "top:96.0px;left:140.0px;");
		
		// player2Label
		player2Label = new Label();
		player2Label.setStyleName("inputLabel");
		player2Label.setImmediate(false);
		player2Label.setWidth("80px");
		player2Label.setHeight("-1px");
		player2Label.setValue("Player 2");
		mainLayout.addComponent(player2Label, "top:102.0px;left:40.0px;");
		
		// dateInput
		dateInput = new InlineDateField();
		dateInput.setImmediate(false);
		dateInput.setWidth("240px");
		dateInput.setHeight("143px");
		dateInput.setInvalidAllowed(false);
		dateInput.setRequired(true);
		dateInput.setValue(new Date());
		mainLayout.addComponent(dateInput, "top:39.0px;left:320.0px;");
		
		// resultLabel
		resultLabel = new Label();
		resultLabel.setStyleName("inputLabel");
		resultLabel.setImmediate(false);
		resultLabel.setWidth("80px");
		resultLabel.setHeight("18px");
		resultLabel.setValue("Result");
		mainLayout.addComponent(resultLabel, "top:161.0px;left:40.0px;");
		
		// resultField
		resultField = new TextField();
		resultField.setImmediate(false);
		resultField.setWidth("-1px");
		resultField.setHeight("-1px");
		resultField.setInvalidAllowed(false);
		resultField.setRequired(true);
		mainLayout.addComponent(resultField, "top:158.0px;left:140.0px;");
		
		// saveButton
		saveButton = new Button();
		saveButton.setCaption("Save");
		saveButton.setImmediate(true);
		saveButton.setWidth("-1px");
		saveButton.setHeight("-1px");
		saveButton.addListener(this);
		mainLayout.addComponent(saveButton, "top:220.0px;left:490.0px;");
		
		testLabel = new Label();
		testLabel.setWidth("600px");
		testLabel.setHeight("300px");
		testLabel.setValue("test");
		mainLayout.addComponent(testLabel, "top:250px;left:20px;");
		
		return mainLayout;
	}

	public void buttonClick(ClickEvent event) {
		if(event.getButton() == saveButton){
			String player1Name = (String) player1Field.getValue();
			String player2Name = (String) player2Field.getValue();
			Date date = (Date)dateInput.getValue();
			String result = (String) resultField.getValue();
			
			Key playerKey = KeyFactory.createKey("Match", player1Name);
			
			Entity matchEntry1 = new Entity("Match", playerKey);
			matchEntry1.setProperty("player1", player1Name);
			matchEntry1.setProperty("player2", player2Name);
			matchEntry1.setProperty("date", date);
			matchEntry1.setProperty("result", result);
			
			playerKey = KeyFactory.createKey("Match", player2Name);
			
			Entity matchEntry2 = new Entity("Match", playerKey);
			matchEntry2.setProperty("player1", player1Name);
			matchEntry2.setProperty("player2", player2Name);
			matchEntry2.setProperty("date", date);
			matchEntry2.setProperty("result", result);
			
			DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
			
			datastore.put(matchEntry1);
			datastore.put(matchEntry2);
		}
		
	}

}
